package com.laicunba.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.pojo.VcoinDO;
import com.laicunba.pojo.VcoinWalletDO;
import com.laicunba.service.VcoinService;
import com.laicunba.service.VcoinWalletService;
import com.laicunba.util.AssertUtil;

@Service
public class VcoinWalletServiceImpl extends BaseServiceImpl implements VcoinWalletService {

	@Autowired
	private VcoinService vcoinService;

	@Override
	public boolean create(String uid) {
		VcoinWalletDO vcoinWalletDO = new VcoinWalletDO();
		vcoinWalletDO.setId(uid);
		vcoinWalletDO.setBalance(0);
		return create(vcoinWalletDO);
	}

	@Transactional
	@Override
	public boolean add(String uid, long amt, String mark, String summary) {
		AssertUtil.assertTrue(amt > 0, "AMT_LESS_THEN_0");
		VcoinWalletDO vcoinWalletDO = get(VcoinWalletDO.class, uid);
		if (vcoinWalletDO == null) {
			AssertUtil.assertTrue(create(uid), "CREATE_VCOIN_WALLET_FAIL");
			vcoinWalletDO = get(VcoinWalletDO.class, uid);
		}
		AssertUtil.assertNotNull(vcoinWalletDO, "VCOIN_WALLET_NOT_EXIT");
		VcoinDO vcoinDO = new VcoinDO();
		vcoinDO.setBalance(vcoinWalletDO.getBalance() + amt);
		vcoinDO.setDr(amt);
		vcoinDO.setCr(0);
		vcoinDO.setMark(mark);
		vcoinDO.setSummary(summary);
		vcoinDO.setWalletId(uid);
		AssertUtil.assertTrue(vcoinService.create(vcoinDO), "CREATE_VCOIN_FAIL");
		vcoinWalletDO.setBalance(vcoinDO.getBalance());
		AssertUtil.assertTrue(updateSkipCheckOwner(vcoinWalletDO), "UPDATE_VCOIN_WALLET_FAIL");
		return true;
	}

	@Transactional
	@Override
	public boolean subtract(String uid, int amt, String mark, String summary) {
		AssertUtil.assertTrue(amt > 0, "AMT_LESS_THEN_0");
		VcoinWalletDO vcoinWalletDO = get(VcoinWalletDO.class, uid);
		if (vcoinWalletDO == null) {
			AssertUtil.assertTrue(create(uid), "CREATE_VCOIN_WALLET_FAIL");
			vcoinWalletDO = get(VcoinWalletDO.class, uid);
		}
		AssertUtil.assertNotNull(vcoinWalletDO, "VCOIN_WALLET_NOT_EXIT");
		AssertUtil.assertTrue(vcoinWalletDO.getBalance()>=amt, "NO_MORE_BALANCE");
		VcoinDO vcoinDO = new VcoinDO();
		vcoinDO.setBalance(vcoinWalletDO.getBalance() - amt);
		vcoinDO.setDr(0);
		vcoinDO.setCr(amt);
		vcoinDO.setMark(mark);
		vcoinDO.setSummary(summary);
		vcoinDO.setWalletId(uid);
		AssertUtil.assertTrue(vcoinService.create(vcoinDO), "CREATE_VCOIN_FAIL");
		vcoinWalletDO.setBalance(vcoinDO.getBalance());
		AssertUtil.assertTrue(updateSkipCheckOwner(vcoinWalletDO), "UPDATE_VCOIN_WALLET_FAIL");
		return true;
	}

	@Override
	public long getBalance(String uid) {
		VcoinWalletDO vcoinWalletDO = get(VcoinWalletDO.class, uid);
		if (vcoinWalletDO != null) {
			return vcoinWalletDO.getBalance();
		}
		return 0;
	}

	@Override
	public List<VcoinDO> list(String uid, int limit) {
		return vcoinService.list(uid, limit);
	}
}
